import numpy
import pandas


class PandasManager(object):
    """
    pandas管理器
    """

    def __init__(self, array):
        self.array = array
        print(f"self.array: \n{self.array}")
        self.pandas_data_frame = pandas.DataFrame(array)
        print(f"self.pandas_data_frame :\n{self.pandas_data_frame}")
        print("-*" * 20)
        index = ["行{}".format(i + 1) for i in range(array.shape[0])]
        self.pandas_data_frame = pandas.DataFrame(array, index=index)
        print(self.pandas_data_frame)
        print("-*" * 20)
        columns = pandas.date_range(start="20190403", periods=array.shape[1], freq="B")
        self.pandas_data_frame = pandas.DataFrame(array, index=index, columns=columns)
        print(self.pandas_data_frame)

    def pandas_property(self):
        """
        pandas的属性
        :return:
        """
        print("===========pandas_property=====================")
        print(f"pandas.shape : {self.pandas_data_frame.shape}")
        print(f"pandas.index : {self.pandas_data_frame.index}")
        print(f"pandas.columns : {self.pandas_data_frame.columns}")
        print(f"pandas.values : \n{self.pandas_data_frame.values}")
        print(f"pandas.T : \n{self.pandas_data_frame.T}")
        print(f"pandas.head() : \n{self.pandas_data_frame.head()}")
        print(f"pandas.tail() : \n{self.pandas_data_frame.tail()}")
        print("===========pandas_property=====================")

    def ranks_change(self):
        """
        行列序号改变
        :return:
        """
        print("===========ranks_change=====================")
        index = ["改行_{}".format(i + 1) for i in range(self.array.shape[0])]
        self.pandas_data_frame.index = index
        print(self.pandas_data_frame)
        print("-*" * 20)
        ss = self.pandas_data_frame.reset_index(drop=True)
        print(ss)
        print("-*" * 20)
        columns = pandas.date_range(start="20190406", periods=self.array.shape[1], freq="B")
        self.pandas_data_frame.columns = columns
        print(self.pandas_data_frame)
        print("===========ranks_change=====================")

    @staticmethod
    def array_multiply():
        """
        乘法
        :return:
        """
        print("===========array_multiply=====================")
        print(numpy.dot(numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]), numpy.array([[1, 2], [3, 4], [5, 6], [7, 8]])))

        print(numpy.matmul(numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]), numpy.array([[1, 2], [3, 4], [5, 6], [7, 8]])))
        print(numpy.dot(numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]), 3))
        print("===========array_multiply=====================")


if __name__ == '__main__':
    pandas_manager = PandasManager(numpy.random.normal(0, 1, (10, 5)))
    pandas_manager.pandas_property()
    pandas_manager.ranks_change()
    print(pandas_manager.pandas_data_frame.loc[pandas_manager.pandas_data_frame.index[0:4], ['2019-04-08']])
    # print(pandas.DataFrame(numpy.zeros((10, 5))).iloc[[0, 1], [0, 1, 2]])
